/**
 * author:Administrator
 * date:2016/3/16
 * desc:TODO
 */
"use strict";

let RawQuery = require('./_RawQuery.js');

class WareQuery extends RawQuery {
  constructor() {
    super();
  }


  * findByCustomer(cateId, locId, pageIndex, pageSize) {
    if (!locId) {
      return yield this._findByCategory(cateId, pageIndex, pageSize);
    } else {
      return yield this._findByCategoryLocation(cateId, locId, pageIndex, pageSize);
    }
  }

  /******************************************************
   *       用户查询商品1: 根据 类型和位置查询商品            *
   ******************************************************/
  * _findByCategoryLocation(categoryId, locationId, pageIndex, pageSize) {
    // ware
    // category
    // 相关的shop
    // 相关shop 的 location
    let sql = "select ware.*, shop.name as shopName, shop.id as shopId, location.name as locName, category.name as cateName " +
      "from ware, shopWare, shop, category, location " +
      "where shopware.categoryId='%categoryId' " +
      "and category.id=shopWare.categoryId " +
      "and location.id=shopWare.locationId " +
      "and shopWare.locationId='%locationId' " +
      "and ware.id=shopWare.wareId " +
      "and shop.id=shopWare.shopId " +
      "limit %start,%end";
    let offset = this.offset(pageIndex, pageSize);
    sql = sql.replace('%locationId', locationId)
      .replace('%categoryId', categoryId)
      .replace('%start', offset.start)
      .replace('%end', offset.end);
    return yield this.query(sql);
  }


  /******************************************************
   *       用户查询商品2: 根据类型查询商品                   *
   ******************************************************/
  * _findByCategory(categoryId, pageIndex, pageSize) {
    // ware
    // category
    // 相关的shop
    // 相关shop 的 location
    let sql = "select ware.*, shop.name as shopName, shop.id as shopId, location.name as locName, category.name as cateName " +
      "from ware, shopWare, shop, category, location " +
      "where shopware.categoryId='%categoryId' " +
      "and category.id=shopWare.categoryId " +
      "and ware.id=shopWare.wareId " +
      "and shop.id=shopWare.shopId " +
      "limit %start,%end";

    let offset = this.offset(pageIndex, pageSize);
    sql = sql.replace('%categoryId', categoryId)
      .replace('%start', offset.start)
      .replace('%end', offset.end);
    return yield this.query(sql);
  }


  /******************************************************
   *                    vendor查询商品列表                *
   ******************************************************/
  * findByVendor(vendorId, pageIndex, pageSize) {
    // ware
    let sql = "select ware.* " +
      "from ware " +
      "where ware.vendorId='%vendorId' " +
      "limit %start,%end";
    let offset = this.offset(pageIndex, pageSize);
    sql = sql.replace('%vendorId', vendorId)
      .replace('%start', offset.start)
      .replace('%end', offset.end);
    return yield this.query(sql);
  }


  /******************************************************
   *                    vendor查询商品详情                *
   ******************************************************/
  * vendorWareDetail(vendorId, wareId) {
    // ware
    // 相关的shop
    // 种类
    let sql = "select ware.*, " +
      " shop.name as shopName, shop.id as shopId, " +
      " category.name as cateName," +
      " location.name as locName " +
      " from `ware`,`shop`,`category`,`location`, `shopware` " +
      " where ware.id='%wareId' " +
      " and ware.vendorId='%vendorId' " +
      " and ware.categoryId = category.id " +
      " and shopware.wareId = ware.id " +
      " and shopware.shopId = shop.id " +
      " and location.id = shop.locationId ";
    sql = sql.replace('%vendorId', vendorId)
      .replace('%wareId', wareId);
    return yield this.query(sql);
  }
}

module.exports = new WareQuery();